package com.kaishengit.service;

import java.util.ArrayList;
import java.util.List;

import com.kaishengit.dao.GoalDao;
import com.kaishengit.dao.MessageDao;
import com.kaishengit.dao.TaskDao;
import com.kaishengit.dao.UserDao;
import com.kaishengit.entity.Goal;
import com.kaishengit.entity.GoalUserTask;
import com.kaishengit.entity.Message;
import com.kaishengit.entity.Task;
import com.kaishengit.entity.User;
import com.kaishengit.entity.UserTask;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.PKutil;
import com.kaishengit.util.SubS;


public class TaskService {
	private TaskDao dao=new TaskDao();
	private GoalDao goalDao=new GoalDao();
	private Task t=new Task();
	private UserDao udao=new UserDao();
	private MessageDao mdao=new MessageDao();
	
	public Goal findById(String goalid) {
		return goalDao.findById(goalid);
	}
	public List<Task> findBygoalId(String goalid){
		return dao.findByGoalId(goalid);
	}
	public void save(String goalid, String name, String starttime,
			String endtime, String desc,String isImport,
			 String userid, String projectid) {
		String taskid= PKutil.getPk();
		String time=DateUtil.getNow();
		if(DateUtil.isBefore(starttime, time)){
			t.setProgress("101");
			t.setStat("未开始");
		}else if(DateUtil.isBefore(time, endtime)){
			t.setProgress("-1");
			t.setStat("已过期");
		}else{
			t.setProgress("0");
			t.setStat("新任务");
		}
		t.setId(taskid);
		t.setName(name);
		t.setStarttime(starttime);
		t.setEndtime(endtime);
		t.setDescription(desc);
		t.setIsImport(isImport);
		t.setT_goal_id(goalid);
		t.setT_user_id(userid);
		t.setT_project_id(projectid);
		dao.save(t);
		
		
		
		Message m=new Message();
		String nowtime=DateUtil.getNow();
		String mid=PKutil.getPk();
		m.setId(mid);
		String descr=SubS.getSubString(desc);
		m.setContent(descr);
		m.setDate(nowtime);
		m.setName(name);
		m.setTaskid(taskid);
		m.setProjectid(projectid);
		m.setUserid(userid);
		m.setType("task");
		mdao.saveTask(m);
	}
	
	
	
	public Task findByTaskId(String taskid) {
		return dao.findByTaskId(taskid);
	}
	
	public void update(String taskid, String name,
			String starttime, String endtime, String desc,
			String isImport, String progress) {
		String stat=null;
		if("0".equals(progress)){
			stat="新任务";
		}else if("100".equals(progress)){
			stat="已完成";
		}else{
			stat="进行中";
		}
		dao.update(name,starttime,endtime,desc,stat,isImport,progress,taskid);
	}
	
	
	public List<UserTask> findBygoal(String goalid){
		List<Task> list=dao.findByGoalId(goalid);
		List<UserTask> lis=new ArrayList<UserTask>();
		for(Task t:list){
			String progress=t.getProgress();
			if("101".equals(progress)){
				if(DateUtil.isBefore(t.getStarttime(), DateUtil.getNow())){
					t.setProgress("101");
				}else if(DateUtil.isBefore( DateUtil.getNow(), t.getEndtime())){
					t.setProgress("-1");
				}else{
					t.setProgress("0");
				}
			}
			UserTask ut=new UserTask();
			ut.setTask(t);
			User user=udao.findById(t.getT_user_id());
			ut.setUser(user);
			lis.add(ut);
		}
		return lis;
		
	}
	public List<UserTask> findByProjectId(String projectid){
		List<Task> list=dao.findByProjectId(projectid);
		List<UserTask> lis=new ArrayList<UserTask>();
		for(Task t:list){
			String progress=t.getProgress();
			if("101".equals(progress)){
				if(DateUtil.isBefore(t.getStarttime(), DateUtil.getNow())){
					t.setProgress("101");
				}else if(DateUtil.isBefore( DateUtil.getNow(), t.getEndtime())){
					t.setProgress("-1");
				}else{
					t.setProgress("0");
				}
			}
			UserTask ut=new UserTask();
			ut.setTask(t);
			User user=udao.findById(t.getT_user_id());
			ut.setUser(user);
			lis.add(ut);
		}
		return lis;
		
	}
	public void del(String taskid) {
		dao.delById(taskid);
	}
	public List<UserTask> findByUserId(String userid) {
		List<Task> list=dao.findByUserId(userid);
		List<UserTask> lis=new ArrayList<UserTask>();
		for(Task t:list){
			String progress=t.getProgress();
			if("101".equals(progress)){
				if(DateUtil.isBefore(t.getStarttime(), DateUtil.getNow())){
					t.setProgress("101");
				}else if(DateUtil.isBefore( DateUtil.getNow(), t.getEndtime())){
					t.setProgress("-1");
				}else{
					t.setProgress("0");
				}
			}
			UserTask ut=new UserTask();
			ut.setTask(t);
			User user=udao.findById(t.getT_user_id());
			ut.setUser(user);
			lis.add(ut);
		}
		return lis;
	}
	public Task findTById(String taskid) {
		return dao.findByTaskId(taskid);
	}
	
	
}
